//
//  NSString+TAMTAttributed.m
//  TAMeetApp
//
//  Created by 九月 on 12/4/24.
//

#import "NSString+TAMTAttributed.h"

@implementation NSString (TAMTAttributed)

- (NSAttributedString *)attributedStringWithlineSpacing:(CGFloat)lineSpacing
                                       textColor:(UIColor *)textColor
                                              font:(UIFont *)font {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    
    NSDictionary *attributes = @{
        NSFontAttributeName: font,
        NSParagraphStyleAttributeName: paragraphStyle,
        NSForegroundColorAttributeName: textColor
    };
    
    return [[NSAttributedString alloc] initWithString:self attributes:attributes];
}


@end


@implementation NSAttributedString (HeightCalculation)

- (CGFloat)heightWithWidth:(CGFloat)width font:(UIFont *)font {
    if (!self.length) return 0;
    
    NSMutableAttributedString *mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self];
    [mutableAttrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.length)];
    
    CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
    CGRect boundingRect = [mutableAttrString boundingRectWithSize:constraint
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                           context:nil];
    
    return ceil(boundingRect.size.height);
}

@end
